//
//  YMToolChooseDateSheet.m
//  YMMedical
//
//  Created by y s on 2023/7/25.
//

#import "YMToolChooseDateSheet.h"

@interface YMToolChooseDateSheet ()

/** 日期选择 */
@property (nonatomic, strong) UIDatePicker *datePicker;

@end


@implementation YMToolChooseDateSheet

- (instancetype)init{
    self = [super init];
    if (self) {
        [self setUI];
    }
    return self;
}

#pragma mark -- UI
- (void)setUI{
    [self.vBg addSubview:self.datePicker];
    [self.datePicker mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.mas_equalTo(0);
        make.bottom.mas_equalTo(-IPHONE_X_BOTTOM_HEIGHT);
    }];
    WEAK_SELF;
    self.sureClickBlock = ^{
        [weakSelf sureClickAction];
    };
}

- (void)sureClickAction{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = [SDataUtil stringOf:self.dateFormat valid:@"yyyy-MM-dd"];
    NSString *dateStr = [formatter  stringFromDate:[_datePicker date]];
    if (self.sureDateBlock) {
        self.sureDateBlock(dateStr);
    }
}
- (void)setMinDate:(NSDate *)minDate{
    _minDate = minDate;
    if (_minDate != nil){
        [_datePicker setMinimumDate:minDate];
    }
}

#pragma mark -- setter and getter
- (UIDatePicker *)datePicker{
    if (_datePicker == nil) {
        _datePicker = [[UIDatePicker alloc] init];
         //设置地区: zh-中国
        if ([YMLanguageUtil isEnglish]){
            _datePicker.locale = [NSLocale localeWithLocaleIdentifier:@"en_US"];
        }else{
            _datePicker.locale = [NSLocale localeWithLocaleIdentifier:@"zh_CN"];
        }
        _datePicker.datePickerMode = UIDatePickerModeDate;
        [_datePicker setDate:[NSDate date] animated:YES];
        [_datePicker setMaximumDate:[NSDate date]];
        
        if (@available(iOS 13.4, *)) {
            _datePicker.preferredDatePickerStyle = UIDatePickerStyleWheels;
        }
//        //监听DataPicker的滚动
//        [_datePicker addTarget:self action:@selector(dateChange:) forControlEvents:UIControlEventValueChanged];
    }
    return _datePicker;
}


@end
